package com.hk.dao.impl;

import com.hk.dao.IProductDao;
import com.hk.model.Product;
import com.hk.utils.JdbcMysql;
import com.hk.utils.SqlSymbol;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author: zhe
 * @Date: 2021/6/22 16:53
 * @Statement: 产品Dao层接口实现类
 */
public class ProductDaoImpl implements IProductDao {

    //    JDBC连接工具类
    private static JdbcMysql jdbcMysql = JdbcMysql.getInstance();
    private static String sql = "";

    @Override
    public ResultSet findAll(Integer page, Integer limit) {
        sql = "select * from product ";
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findAll(int page, int limit, int tMenuId) {
        sql = "select * from product where 1=1 ";
        if (tMenuId > 0) {
            sql += " and menu_id = "+tMenuId+" or tmenu_id = "+tMenuId;
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet count(Integer page, Integer limit) {
        sql = "select count(1) from product where 1=1 ";
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet count(int page, int limit, int tMenuId) {
        sql = "select count(1) from product where 1=1 ";
        if (tMenuId > 0) {
            sql += " and menu_id = "+tMenuId+" or tmenu_id = "+tMenuId;
        }
        if (page >= 0 && limit > 0) {
            sql += " limit "+page+SqlSymbol.COMMA.getValue()+limit;
        }
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findById(Integer id) {
        sql = "select * from product where id = "+id;
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findById(int id) {
        sql = "select * from product where id in ((SELECT id FROM product WHERE id < "+id+" ORDER BY id DESC LIMIT 1)"+SqlSymbol.COMMA.getValue()+
                id+SqlSymbol.COMMA.getValue()+"(SELECT id FROM product WHERE id > "+id+" ORDER BY id ASC LIMIT 1)"+SqlSymbol.RBRACKET.getValue();
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public ResultSet findByImgUrl(String imgUrl) {
        sql = "select * from product where img = "+SqlSymbol.MARK.getValue()+imgUrl+SqlSymbol.MARK.getValue();
        ResultSet resultSet = jdbcMysql.jdbcQuery(sql);
        return resultSet;
    }

    @Override
    public int save(Product t) {
        return 0;
    }

    @Override
    public int update(Product t) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }


}
